package com.corner.ui.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.App;
import com.corner.app.ConsTants;
import com.corner.model.cornerservice.domain.Info;
import com.corner.model.cornerservice.domain.Like;
import com.corner.presenter.imp.IntersectionPresenter;
import com.corner.ui.adapter.IntersectionAdapter;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.view.IItersectionView;
import com.corner.utils.AppManager;
import com.corner.utils.ToastUtil;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;
import com.corner.utils.view.MainActionBar;

import java.util.List;

/**
 * Created by Administrator on 2015/5/5.
 */
public class IntersectionActivity extends BaseActivity<IntersectionPresenter> implements View.OnClickListener, IItersectionView {
    public static final String INFO_ID = "infoId";
    private MainActionBar actionBar;

    @FindView(R.id.gv_intersection_agree_people)
    private GridView gvAgree;
    @FindView(R.id.gv_intersection_watch_people)
    private GridView gvWatch;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_intersection_agree_people)
    private TextView tvAgree;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_intersection_watch_people)
    private TextView tvWatch;

    private IntersectionAdapter agreeAdapter;
    private IntersectionAdapter watchAdapter;

    @Override
    protected IntersectionPresenter createP() {
        return new IntersectionPresenter();
    }

    @Override
    protected void setOnClick() {
        actionBar.btnLeft.setOnClickListener(this);
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initData() {
        Intent intent = getIntent();
        final Long infoId = intent.getLongExtra(INFO_ID, 0);
        excuteTaskWhenNetOk(new Task() {
            @Override
            public void onExcute() {
                Info info = Info.createWithoutData(infoId, Info.class);
                presenter.requestAgreeUsers(info);
                presenter.requestReadedUsers(info);
            }
        });
    }

    /**
     * 设置gridview的高度固定
     *
     * @param gridview
     * @param likes
     */
    private void setGridViewHeight(GridView gridview, List<Like> likes) {
        int lines = likes.size() / getResources().getInteger(R.integer.intersetion_line_item_count) + 1;
        int height = lines * getResources().getDimensionPixelSize(R.dimen.dimen_intersetion_item_width_height) + getResources().getDimensionPixelSize(R.dimen.dimen_intersection_gridview_vertical_spacing) * (lines - 1) + gridview.getPaddingBottom() + gridview.getPaddingTop();
        gridview.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
    }

    @Override
    public void initView() {
        super.initView();
        actionBar = new MainActionBar(this);
        actionBar.initActionBarButton();
        actionBar.btnLeft.setVisibility(View.VISIBLE);
        actionBar.btnLeft.setText(getString(R.string.intersection));
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_intersection);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_actionbar_left:
                AppManager.getInstance().finishActivity(this);
                break;
        }
    }

    @Override
    public void showReadedUsers(List<Like> likes) {
        if (likes != null && likes.size() > 0) {
            watchAdapter = new IntersectionAdapter(IntersectionActivity.this, likes);
            gvWatch.setVisibility(View.VISIBLE);
            tvWatch.setVisibility(View.VISIBLE);
            gvWatch.setAdapter(watchAdapter);
            setGridViewHeight(gvWatch, likes);
        }
    }

    @Override
    public void showAgreeUsers(List<Like> likes) {
        if (likes != null && likes.size() > 0) {
            agreeAdapter = new IntersectionAdapter(IntersectionActivity.this, likes);
            gvAgree.setVisibility(View.VISIBLE);
            tvAgree.setVisibility(View.VISIBLE);
            gvAgree.setAdapter(agreeAdapter);
            setGridViewHeight(gvAgree, likes);
        }
    }

    @Override
    public void showRequestError(String message) {
        ToastUtil.showCustomToastInRelease(App.application, message);
    }
}
